/*
 * @Author: lclangcheng
 * @Date: 2018-11-06 11:07:12
 * @Last Modified by: lclangcheng
 * @Last Modified time: 2019-12-25 15:31:51
 */
'use strict'

const JMQ = require('jmq')
const log = require('../libs/logger')
const config = require('config')
const utils = require('../libs/utils')

const className = 'MsgCenter'
const BaseService = require('./base_service')

class MsgCenter extends BaseService {
  static async emit (strAppIdentity, strTopic, objData) {
    let appId = strAppIdentity
    const topic = strTopic
    const data = objData

    if (utils.dataType(data) !== 'Object') {
      log.warn({ className, func: 'emit', topic, data, err: 'data is not Object' })
      return
    }

    if (!appId) {
      appId = config.kMyMqAppIdentity
    }

    // log.info(`${className} emit:`, { topic, data })
    log.info({ className, appId, topic, emit: JSON.stringify(data) })
    await this.mq.emit(appId, topic, JSON.stringify(data))
  }
}

MsgCenter.mq = new JMQ(config.kMyMqAppIdentity)

module.exports = MsgCenter
